package com.yznmall.mall.service.impl;

import com.yznmall.mall.dao.ShopDao;
import com.yznmall.mall.dto.ShopExecution;
import com.yznmall.mall.entity.Shop;
import com.yznmall.mall.enums.ShopStateEnum;
import com.yznmall.mall.exceptions.ShopOperationException;
import com.yznmall.mall.service.ShopService;
import com.yznmall.mall.util.ImageUtil;
import com.yznmall.mall.util.PageCalculator;
import com.yznmall.mall.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopDao shopDao;

    @Override
    public ShopExecution getShopList(Shop shopcondition, int pageIndex, int pageSize) {
            int rowIndex = PageCalculator.caculatorRowIndex(pageIndex,pageSize);
            List<Shop> shopList = shopDao.queryShopList(shopcondition,pageIndex,pageSize);
            int count = shopDao.queryShopCount(shopcondition);
            ShopExecution shopExecution =new ShopExecution();
            if(shopList != null){
                shopExecution.setShopList(shopList);
                shopExecution.setCount(count);
            }else{
                shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
            }
            return shopExecution;

    }

    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, InputStream shopImgInputStream,String imgName) {
        if(shop == null){
            return new ShopExecution(ShopStateEnum.NULL_SHOPID);
        }
        try{
            shop.setEnableStatus(0);
            shop.setLastEditTime(new Date());
            shop.setCreateTime(new Date());
            int effectNum = shopDao.insertShop(shop);
            if(effectNum < 0){
                //使用ShopOperationException才能使事务终止并回滚,使用Exception不会使事务回滚
                throw new ShopOperationException("店铺创建失败");
            }else{
                if(shopImgInputStream !=null){
                    try{
                        addShopImg(shop, shopImgInputStream,imgName);
                    }catch(Exception e){
                        throw new ShopOperationException("addShopImg error:"+e.getMessage());
                    }
                    effectNum = shopDao.updateShop(shop);
                    if(effectNum <= 0)
                    {
                        throw new ShopOperationException("更新图片地址失败");
                    }
                    
                }
            }

        }catch(Exception e){
            throw new ShopOperationException("addShop error :"+e.getMessage());
        }

        return new ShopExecution(ShopStateEnum.CHECK,shop);
    }

    @Override
    public Shop getShopById(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    @Override
    public ShopExecution modifyShop(Shop shop, InputStream shopimgInputStream, String fileName) throws ShopOperationException {
        //分两步1、判断是否需要处理图片2、进行店铺更新
        if(shop == null || shop.getShopId() == null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else {
            try {
                if (shopimgInputStream != null && fileName != null && !"".equals(fileName)) {
                    Shop tempShop = shopDao.queryByShopId(shop.getShopId());
                    if (tempShop.getShopImg() != null) {
                        ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    addShopImg(shop, shopimgInputStream, fileName);
                }
                shop.setLastEditTime(new Date());
                int effectNum = shopDao.updateShop(shop);
                if (effectNum <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopDao.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }

            } catch (Exception e) {
                throw new ShopOperationException("modify error:"+e.getMessage());
            }


        }

    }

    private void addShopImg(Shop shop, InputStream shopImgInputStream,String fileName) {
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImgInputStream,fileName,dest);
        shop.setShopImg(shopImgAddr);
        
    }
}
